package cn.com.cetccst.location.peroid;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

import cn.com.cetccst.datacenter.collection.CollectionRepository;
import cn.com.cetccst.datacenter.collection.entity.LocationRecordEntity;
import cn.com.cetccst.location.LocationListener;
import cn.com.cetccst.location.LocationService;

public class PeroidLocateWorker extends Worker implements LocationListener {
    public PeroidLocateWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

    @NonNull
    @Override
    public Result doWork() {
        Log.i(PeroidLocateHelper.TAG, "do peroid locate worker" );
        //1. get current location
        LocationService.getInstance().addLocationListener(this);
        LocationService.getInstance().start();
        return Result.success();
    }

    @Override
    public void onLocationChanged(LocationRecordEntity locationRecord) {
        Log.i(PeroidLocateHelper.TAG, "location changed in peroid locate worker : " + locationRecord.toJson());
        LocationService.getInstance().removeLocationListener(this);
        //2. save location cache to db
        LocationService.getInstance().getPeroidLocateHelper().setLastReportTime(System.currentTimeMillis());
        CollectionRepository.getInstance().getLocationRecordDao().insertWithCleanup(locationRecord);
        //3. post all location record to server

        //4. clear location db
//        SSRespository.getInstance().getLocationRecordDao().clear();
    }
}
